/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlteraValor.java
 *
 * Created on 17/09/2011, 15:12:01
 */

package View.ProducaoValores;

import Controle.Controlador;
import Controle.Uteis;
import Modelo.Cliente;
import Modelo.Cliente_has_Seguro;
import Modelo.Fisico;
import Modelo.Juridico;
import View.Fundo2;
import View.Progress;
import View.TelaMetodos;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Silvio
 */
public class AlteraValor extends javax.swing.JFrame {
public static Fundo2 pnlFundo = new Fundo2();
    /** Creates new form AlteraValor */
    
    private Cliente cli;

    public AlteraValor() {
         initComponents();
         this.pack();
         this.setLayout(new BorderLayout());
         this.add(pnlFundo, BorderLayout.CENTER);
         this.setExtendedState(MAXIMIZED_BOTH);
    }
    public AlteraValor(Cliente cli){
        this();
        this.cli = cli;
        preencher(cli);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cliente = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        dataInicio = new javax.swing.JFormattedTextField();
        dataFim = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jaPago = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        pago = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        vAreceber = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        vFatur = new javax.swing.JFormattedTextField();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/money.png"))); // NOI18N
        jLabel1.setText("Faturar Valor");

        jLabel3.setText("Cliente:");

        cliente.setDisabledTextColor(new java.awt.Color(51, 51, 51));
        cliente.setEnabled(false);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/alterar.png"))); // NOI18N
        jButton1.setText("Faturar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/voltar.png"))); // NOI18N
        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setText("Data de Inicio:");

        jLabel4.setText("Data Final:");

        dataInicio.setFormatterFactory((new javax.swing.text.DefaultFormatterFactory(Uteis.Mascara("##/##/####"))));

        dataFim.setFormatterFactory((new javax.swing.text.DefaultFormatterFactory(Uteis.Mascara("##/##/####"))));

        jLabel5.setText("Valor já pago:");

        jaPago.setBackground(java.awt.Color.white);
        jaPago.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getCurrencyInstance())));
        jaPago.setDisabledTextColor(java.awt.Color.black);
        jaPago.setEnabled(false);

        jLabel6.setText("Faturar:");

        pago.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,###.00"))));

        jLabel7.setText("Valor a Receber:");

        vAreceber.setBackground(java.awt.Color.white);
        vAreceber.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getCurrencyInstance())));
        vAreceber.setDisabledTextColor(java.awt.Color.black);
        vAreceber.setEnabled(false);

        jLabel8.setText("Valor total da Fatura:");

        vFatur.setBackground(java.awt.Color.white);
        vFatur.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getCurrencyInstance())));
        vFatur.setDisabledTextColor(java.awt.Color.black);
        vFatur.setEnabled(false);

        jButton3.setText("Buscar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(jLabel1))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(jLabel2)
                .addGap(160, 160, 160)
                .addComponent(jLabel4))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(dataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(81, 81, 81)
                .addComponent(dataFim, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(jButton3))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(jLabel5)
                .addGap(163, 163, 163)
                .addComponent(jLabel7))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(jaPago, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80)
                .addComponent(vAreceber, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(jLabel8))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(vFatur, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(jLabel6))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(pago, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(285, 285, 285)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(78, 78, 78)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jLabel3))
                    .addComponent(cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dataFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jButton3)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jaPago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vAreceber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(jLabel8)
                .addGap(18, 18, 18)
                .addComponent(vFatur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(jLabel6)
                .addGap(6, 6, 6)
                .addComponent(pago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(82, 82, 82)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2)))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1024)/2, (screenSize.height-760)/2, 1024, 760);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      if(Uteis.transformaFloat(pago.getText())<=Uteis.transformaFloat(vAreceber.getText())){
          
            String result = Controlador.faturar(s,pago.getText());
            JOptionPane.showMessageDialog(null,result);            
      }else
          JOptionPane.showMessageDialog(null, "valor pago maior que o valor da fatura");
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
            TelaMetodos.Dispose(this);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
          final Progress p = new Progress();
          p.setVisible(true);
          SwingWorker worker = new SwingWorker(){
               @Override
               protected Object doInBackground() throws Exception {
                    preenche(); // meu processo demorado
                    return null;
               }
               @Override
               protected void done() {
                    p.setVisible(false);
               }
          };
          worker.execute();
    }//GEN-LAST:event_jButton3ActionPerformed

    public void preenche(){
         float tempJaPago=0,tempFatura=0;
        s.clear();
        s.addAll(Controlador.buscarSegurosData(cli,dataInicio.getText(),dataFim.getText()));

        for(Cliente_has_Seguro seg:s){
            tempJaPago += seg.getValorJaPago();
            tempFatura += seg.getValorFatura();
        }
        jaPago.setText(Uteis.Num(tempJaPago));
        vFatur.setText(Uteis.Num(tempFatura));
        float receber = tempFatura - tempJaPago;
        vAreceber.setText(Uteis.Num(receber));
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AlteraValor().setVisible(true);
            }
        });
    }
    private ArrayList <Cliente_has_Seguro> s = new ArrayList<Cliente_has_Seguro>();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cliente;
    private javax.swing.JFormattedTextField dataFim;
    private javax.swing.JFormattedTextField dataInicio;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JFormattedTextField jaPago;
    private javax.swing.JFormattedTextField pago;
    private javax.swing.JFormattedTextField vAreceber;
    private javax.swing.JFormattedTextField vFatur;
    // End of variables declaration//GEN-END:variables

    private void preencher(Cliente cli) {
        if(cli instanceof Juridico){
            Juridico j = (Juridico)cli;
            cliente.setText(j.getPessoa().getNome());
        }else if (cli instanceof Fisico){
            Fisico f = (Fisico)cli;
            cliente.setText(f.getPessoa().getNome());
        }
    }

}
